package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;


import com.example.myapplication.dao.FindorSort;
import com.example.myapplication.dao.TxtDao;
import com.example.myapplication.entity.Book;
import com.example.myapplication.ui.dashboard.SortFragment;

import java.io.File;
import java.io.FileNotFoundException;


public class BookDetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        setContentView(R.layout.read);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        ((ImageView) findViewById(R.id.book_cover)).setImageBitmap(SortFragment.storedPic);
        ((TextView) findViewById(R.id.book_name)).setText(SortFragment.storedBook.getName());
        ((TextView) findViewById(R.id.book_author)).setText("作者：" + SortFragment.storedBook.getAuthor());
        ((TextView) findViewById(R.id.book_sort)).setText("类型：" + SortFragment.storedBook.getSort());
        ((TextView) findViewById(R.id.book_detail)).setText(SortFragment.storedBook.getDetail());
        TextView textView2 = findViewById(R.id.tv_download);
        TextView textView = findViewById(R.id.tv_collect);
        boolean iscollected = false;
        File file = new File("/data/data/" + getPackageName() + "/files/" + SortFragment.storedBook.getId() + ".txt");
        System.out.println("/data/data/" + getPackageName() + "/files" + SortFragment.storedBook.getId() + ".txt");
        Handler handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {

                Toast.makeText(getApplicationContext(), "下载完成", Toast.LENGTH_SHORT).show();
                textView2.setClickable(false);
                textView2.setText("已下载");
            }
        };
        Handler handler2 = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {

                Toast.makeText(getApplicationContext(), "下载完成", Toast.LENGTH_SHORT).show();
                textView2.setClickable(false);
                textView2.setText("已下载");
                Intent intent = new Intent(getApplicationContext(), TextActivity.class);
                intent.putExtra("id", SortFragment.storedBook.getId());
                startActivity(intent);
            }
        };
        if (file.exists()) {
            textView2.setText("已下载");
            textView2.setClickable(false);
        }
        Button button = findViewById(R.id.btn_read);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //阅读
                if (!file.exists()) {

                    new TxtDao().getTxt(String.valueOf(SortFragment.storedBook.getId()), getApplicationContext(), handler2);
                    Toast.makeText(getApplicationContext(), "下载中....", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(getApplicationContext(), TextActivity.class);
                    intent.putExtra("id", SortFragment.storedBook.getId());
                    startActivity(intent);
                }

            }
        });

        for (Book book : FindorSort.collectedBook) {
            if (book.getId() == SortFragment.storedBook.getId()) {
                textView.setText("已收藏");
                textView.setClickable(false);
                iscollected = true;
                break;
            }
        }


        if (!iscollected)
            textView.setOnClickListener(v -> {
                //收藏

                FindorSort.collectedBook.add(SortFragment.storedBook);
                FindorSort.collectedPic.add(SortFragment.storedPic);

                if (!file.exists()) {

                    new TxtDao().getTxt(String.valueOf(SortFragment.storedBook.getId()), getApplicationContext(), handler);
                    Toast.makeText(getApplicationContext(), "下载中....", Toast.LENGTH_SHORT).show();
                }

                textView.setText("已收藏");
                textView.setClickable(false);

            });






          /*  textView2.setOnClickListener(v -> {
                //下载

                new TxtDao().getTxt(String.valueOf(SortFragment.storedBook.getId()), getApplicationContext(), handler);
                Toast.makeText(getApplicationContext(), "下载中....", Toast.LENGTH_SHORT).show();
            });*/

    }
}


